package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.contract.bean.SettingRuleBean;
import com.wwwarehouse.contract.contract.ReleaseAddingPublishingRulesFragment;
import com.wwwarehouse.contract.event.DeleteEvent;

import java.util.ArrayList;
import java.util.List;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/10/19.
 *         <p/>
 *         设置商品规则
 */

public class SettingRulesGoodsAdapter extends RecyclerView.Adapter<SettingRulesGoodsAdapter.ViewHolder> {

    private Context mContext = null;
    private int mHeight = 0;
    private int mWidth = 0;
    private OnItemClickListener mListener = null;
    private ArrayList<SettingRuleBean.DataBean.ListBean> tagList = new ArrayList<>();
    private int pos;
    private String pbUkid;
    private List<SettingRuleBean.DataBean.ListBean> selectList;
    private boolean isSelected = false;
    private int numOfPage;
    private PopupWindow popupWindow;

    public SettingRulesGoodsAdapter(Context context, ArrayList<SettingRuleBean.DataBean.ListBean> list, int height, int Width, int numOfPage, int pos, String pbUkid) {
        this.mContext = context;
        this.mHeight = height;
        this.mWidth = Width;
        this.numOfPage = numOfPage;
        this.tagList = list;
        this.pos = pos;
        this.pbUkid = pbUkid;
        this.selectList = new ArrayList<>();

    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_setting_goods_rule_adapter, parent, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        GridLayoutManager.LayoutParams params = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
        params.height = (mHeight - ConvertUtils.dip2px(mContext, 50)) / numOfPage; //RecyclerView高度减去距离按钮的高度
        //获取当前控件的布局对象
//        params.width = (int) ((mWidth - 4* ConvertUtils.dip2px(mContext, 10)) /3L);//设置当前控件布局的宽度和margin
        holder.itemView.setLayoutParams(params);
        holder.tvGoodName.setText(tagList.get(position).getName());
        try {
            if (tagList.get(position).getImg().isEmpty()) {
                holder.ivGoodsPic.setImageResource(R.drawable.pic_not_found);
            } else {
                ImageloaderUtils.getImageLoader().displayImage(tagList.get(position).getImg(), holder.ivGoodsPic);
            }
        } catch (Exception e) {
            holder.ivGoodsPic.setImageResource(R.drawable.pic_not_found);
        }
        if (tagList.get(position).getQty() != null) {
            holder.mGoodsSale.setText("库存:"+tagList.get(position).getQty()+tagList.get(position).getUnit());
        } else {
            holder.mGoodsSale.setText("库存:" + tagList.get(position).getUnit());
        }
        if (0 != tagList.get(position).getPrice()) {
            holder.tvDetails.setText("¥" + tagList.get(position).getPrice() + "");
        } else {
            holder.tvDetails.setText("¥");
        }
        holder.mCode.setText(tagList.get(position).getBarCode());
        if (tagList.get(position).getPriceRuleType() != null) {
            tagList.get(position).setHasSetRule(true);
        }

        if (tagList.get(position).isHasSetRule()) {
            holder.mSetRule.setSelected(true);
            holder.mSetRule.setBackgroundResource(R.drawable.publish_time_unclick_background);
            holder.mSetRule.setText("已设置规则");
        } else {
            holder.mSetRule.setSelected(false);
            holder.mSetRule.setBackgroundResource(R.drawable.publish_time_normal_background);
            holder.mSetRule.setText("设置规则");
        }
        holder.mSetRule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转设置规则界面
                ReleaseAddingPublishingRulesFragment rulesFragment = new ReleaseAddingPublishingRulesFragment();
                Bundle b = new Bundle();
                b.putSerializable("data", tagList.get(position));
                b.putInt("position", position);
                b.putInt("pos", pos);
                b.putString("pbUkid", pbUkid);
                rulesFragment.setArguments(b);
                ((BaseCardDeskActivity) mContext).pushFragment(rulesFragment);
            }
        });

        holder.itemView.setTag(tagList.get(position));
        holder.mPoints.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shwoPopupWindow(holder.rlDelete, tagList.get(position).getItemTempUkid());
            }
        });
        holder.rlDelete.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                shwoPopupWindow(v, tagList.get(position).getItemTempUkid());
                return true;
            }
        });
    }

    private void shwoPopupWindow(View view, final String itemTempUkid) {
        int[] location = new int[2];
        view.getLocationInWindow(location);
        View popupView = View.inflate(mContext, R.layout.delete_item_popupwindow_layout, null);
        popupWindow = new PopupWindow(popupView, view.getWidth(), view.getHeight() - 20);
        Drawable drawable = new ColorDrawable(Color.TRANSPARENT);
        LinearLayout mDeleteLayout = (LinearLayout) popupView.findViewById(R.id.delete_layout);
        mDeleteLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                EventBus.getDefault().post(new DeleteEvent(itemTempUkid));

            }
        });
        popupWindow.setBackgroundDrawable(drawable);
        popupWindow.setOutsideTouchable(true);
        int x = location[0];
        int y = location[1];
        popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
    }

    @Override
    public int getItemCount() {
        return tagList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvGoodName;
        TextView mGoodsSale;
        TextView tvDetails;
        ImageView ivGoodsPic;
        TextView mSetRule;
        TextView mCode;
        RelativeLayout rlDelete;
        ImageView mPoints;

        public ViewHolder(View view) {
            super(view);
            tvGoodName = (TextView) view.findViewById(R.id.tv_goods_name);
            mGoodsSale = (TextView) view.findViewById(R.id.tv_goods_sale);
            tvDetails = (TextView) view.findViewById(R.id.tv_shop_name);
            ivGoodsPic = (ImageView) view.findViewById(R.id.iv_goods_pic);
            mCode = (TextView) view.findViewById(R.id.tv_goods_code);
            mSetRule = (TextView) view.findViewById(R.id.img_edit);
            rlDelete = (RelativeLayout) view.findViewById(R.id.rl_delete);
            mPoints = (ImageView) view.findViewById(R.id.img_points);
        }
    }

    public interface OnItemClickListener {
        void ItemClickListener(List<SettingRuleBean.DataBean.ListBean> tagList);
    }

    public List<SettingRuleBean.DataBean.ListBean> getSelectData() {
        return selectList;
    }

    public void dissPop() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }

    }
}
